import { ModelInheritor } from './index'
export async function test() {
  console.log('test start ')

  const mp = new ModelInheritor(m)

  const mCase = {}
  const runner = mp.createRunner(mCase)

  console.log(runner)

  const data = runner.mergeSecondValue('data')
  Object.assign(mCase, data)

  const methods = runner.mergeSecondFun('methods')
  Object.assign(mCase, methods)

  console.log('------------ mCase')
  console.log(runner.applyLast('code'))
  console.log(mCase)

  runner.fireAwait('init')
  await asyncTimeout(1000)
  runner.fireAwait('ready')

  // await asyncTimeout(100)

  // console.log('------ ready')
  // mCase.fun1()

  // runner.applyLast('fun1', ['d1', 'd2'])
  // runner.fireAwait('async1', ['d1', 'd2'])

  // console.log('----- mergeJso')
  // console.log('mergeJsoValue', runner.mergeJsoValue('methods'))
  // console.log('mergeSecondFun', runner.mergeSecondFun('methods'))
  // console.log('mergeJsoMix', runner.mergeJsoMix('methods'))
}

setTimeout(() => {
  test()
}, 1000)

const base = {
  mergeSeconds: ['data']
}

const m1 = {
  code: 'test',
  extends: [base],
  data: { info: 'i1', info2: 'i2' },
  init() {
    console.log(`[m1] init`)
  },
  ready() {
    console.log(`[m1] ready`)
  },
  methods: {
    fun1() {
      console.log('m1 run fun1')
    },
    fun2() {
      console.log('m1 run fun2')
    }
  }
}

const m2 = {
  data() {
    return { info3: 'i3' }
  },
  init() {
    console.log(`[m2] init`)
  },
  ready() {
    console.log(`[m2] ready`)
  },
  methods: {
    fun1() {
      console.log('m run fun1')
      console.log('m to  $upper')
      this.$upper()
    }
  }
}

const m3 = {
  name: 'm3',
  i: 'm3',
  data: {
    a: 100,
    a1: 101,
    a2: 202
  }
}

const m31 = {
  name: 'm3',
  i: 'm31',
  modelMerge: true,
  data: {
    a: 200,
    a1: 201,
    a3: 203
  },
  kkkkkk2: 200
}

const m33 = {
  name: 'm3',
  i: 'm33',
  modelMerge: true,
  data: {
    a: 300,
    a1: 301,
    a4: 304
  },
  kkkkkk2: 200
}
const m34 = {
  name: 'm3',
  i: 'm34',
  modelMerge: true,
  data: {
    a: 400,
    a1: 401,
    a4: 404
  },
  kkkkkk2: 200
}

const m4 = {
  name: 'm4',
  extends: [m3]
}

const m5 = {
  name: 'm5',
  extends: [m33, m34]
}

const m = {
  code: 'test-m',
  extends: [m1, m2, m4, m31, m5],
  data: { info: 'i' },
  init() {
    console.log(`[m] init`)
  },
  ready() {
    console.log(`[m] ready`)
  },
  methods: {}
}

// const m1 = {
//   aaa: 'm1'
// }

// const m21 = { aaa: 'm21' }

// const m2 = {
//   aaa: 'm2',
//   extends: m21,

//   async async1(...datas) {
//     console.log('fire m2 ', datas)
//   }
// }

// const m001 = {
//   aaa: 'm001'
// }

// const m002 = {
//   aaa: 'm002',
//   name: 'm002'
// }

// const m002_1 = {
//   aaa: 'm002_1',
//   name: 'm002'
// }

// const m3 = {
//   aaa: 'm3',
//   extends: [m001, m002],
//   fun1() {
//     console.log('run fun1 m3')
//     return 'm3'
//   },
//   fun2() {
//     console.log('run fun2 m3')
//     return 'm3-2'
//   },
//   async1(...datas) {
//     console.log('fire m3 ', datas)
//   },
//   methods: {
//     m_fun1() {
//       console.log('run m_fun1 m3')
//     }
//   }
// }
// const m4 = {
//   aaa: 'm4',
//   extends: [m001],
//   fun1(p1, p2, p3) {
//     console.log('run fun1 m4')
//     console.log('param m4', p1, p2, p3)
//     return 'm4 '
//   },
//   fun2() {
//     console.log('run fun2 m4')

//     console.log('start m4 upper')
//     console.log(this.$upper())
//     return 'm4-2'
//   },
//   async async1(...datas) {
//     await asyncTimeout(3000)
//     console.log('fire m4 ', datas)
//   },
//   methods: {
//     m_fun1() {
//       console.log('run m_fun1 m4')
//     },
//     m_fun2() {
//       console.log('run m_fun2 m4')
//     }
//   }
// }

// const m = {
//   aaa: 'm',
//   extends: [m1, m2, m3, m4, m002_1],
//   fun1(p1, p2, p3) {
//     console.log('---------------------')
//     console.log('run fun1 m')
//     console.log('param m', p1, p2, p3)
//     console.dir(this.$upper)
//     console.log(this.$upper('1', '2'))
//     console.log(this.$runner.applyLast('fun2'))
//   },
//   methods: {
//     m_fun2: true,
//     m_fun3: '3333'
//   }
// }

async function asyncTimeout(timeout) {
  return new Promise((resolve) => setTimeout(() => resolve(), timeout))
}
